/*
 *
 */

package com.hlingsoft.kkh.action;

import com.hlingsoft.kkh.dao.AdminApplicationDao;
import com.hlingsoft.kkh.dao.AdminJobDao;
import com.hlingsoft.kkh.dao.AdminResumeDao;
import com.hlingsoft.kkh.dao.MerchantDao;
import com.hlingsoft.kkh.entity.ApplicationHistory;
import com.hlingsoft.kkh.entity.Job;
import com.hlingsoft.kkh.entity.MerchantInfo;
import com.hlingsoft.kkh.entity.Resume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * 职位申请记录
 *
 * @author Herbert
 */
@Controller
@RequestMapping("/application")
public class ApplicationAction extends BaseAction {
    @Autowired
    private AdminApplicationDao applicationDao;

    @Autowired
    private AdminJobDao adminJobDao;

    @Autowired
    private MerchantDao merchantDao;

    @Autowired
    private AdminResumeDao resumeDao;

    private static final int rows = 10;


    /**
     * @param status
     * @param userId
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping(value = "/appUserList.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> appUserList(
            @RequestParam(value = "status") String status,
            @RequestParam(value = "userId") long userId,
            @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<ApplicationHistory> applicationHistoryList = new ArrayList<ApplicationHistory>();
            //判断信息状态，all列出所有的，status列出状态对应的
            if (!status.equals("DONE")) {
                //获取所有申请记录
                applicationHistoryList = applicationDao.listByUserNotDone(userId, pageNum * rows, rows);
                for (ApplicationHistory applicationHistory : applicationHistoryList) {

                    //获取每一条记录的职位信息
                    Job job = adminJobDao.getJobById(applicationHistory.getJobId());
                    applicationHistory.setJob(job);
                    //获取每条记录的商户信息
                    MerchantInfo merchantInfo = merchantDao.getMerchantById(job.getMerchantId());
                    applicationHistory.setMerchant(merchantInfo);


                    //获取每一条记录的简历信息
                    Resume resume = resumeDao.getResumeById(applicationHistory.getResumeId());
                    applicationHistory.setResume(resume);
                }
                resultMap.put("applicationList", applicationHistoryList);
            } else {
                applicationHistoryList = applicationDao.listByUserIdByStatus(userId, status, pageNum * rows, rows);
                for (ApplicationHistory applicationHistory : applicationHistoryList) {
                    Job job = adminJobDao.getJobById(applicationHistory.getJobId());
                    applicationHistory.setJob(job);
                    MerchantInfo merchantInfo = merchantDao.getMerchantById(job.getMerchantId());
                    applicationHistory.setMerchant(merchantInfo);

                    Resume resume = resumeDao.getResumeById(applicationHistory.getResumeId());
                    applicationHistory.setResume(resume);

                }
                resultMap.put("applicationList", applicationHistoryList);
            }


            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }

    /**
     * 商户申请记录
     *
     * @param status
     * @param merchantId
     * @param pageNum
     * @param request
     * @return
     */
    @RequestMapping(value = "/merchantList.json", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> list(
            @RequestParam(value = "status") String status,
            @RequestParam(value = "merchantId") long merchantId,
            @RequestParam(value = "pageNum", defaultValue = "0") int pageNum,
            HttpServletRequest request) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            List<ApplicationHistory> applicationHistoryList = new ArrayList<ApplicationHistory>();
            if (!status.equals("DONE")) {
                applicationHistoryList = applicationDao.listByMerchantNotDone(merchantId, pageNum * rows, rows);
                for (ApplicationHistory applicationHistory : applicationHistoryList) {
                    MerchantInfo merchantInfo = merchantDao.getMerchantById(applicationHistory.getMerchantId());
                    applicationHistory.setMerchant(merchantInfo);
                    Job job = adminJobDao.getJobById(applicationHistory.getJobId());
                    applicationHistory.setJob(job);
                    Resume resume = resumeDao.getResumeById(applicationHistory.getResumeId());
                    applicationHistory.setResume(resume);
                }
                resultMap.put("applicationList", applicationHistoryList);
            } else {
                applicationHistoryList = applicationDao.listByMerchantIdByStatus(merchantId, status, pageNum * rows, rows);

                for (ApplicationHistory applicationHistory : applicationHistoryList) {
                    MerchantInfo merchantInfo = merchantDao.getMerchantById(applicationHistory.getMerchantId());
                    applicationHistory.setMerchant(merchantInfo);
                    Job job = adminJobDao.getJobById(applicationHistory.getJobId());
                    applicationHistory.setJob(job);
                    Resume resume = resumeDao.getResumeById(applicationHistory.getResumeId());
                    applicationHistory.setResume(resume);

                }
                resultMap.put("applicationList", applicationHistoryList);
            }
            resultMap.put("result", true);

        } catch (Exception e) {
            e.printStackTrace();
            resultMap.put("result", false);
        }

        return resultMap;

    }


}
